//
//  MCSearchChargeInViewController.m
//  ERP
//
//  Created by QA on 16/1/22.
//  Copyright © 2016年 ShangHai HaoLei Software Technology Co., Ltd. All rights reserved.
//

#import "MCSearchChargeInViewController.h"

#import "GEInputTableViewCell.h"
#import "MTCSelectClienterViewController.h"
#import "MTCConcreteConfirmationViewController.h"

#import "NSDictionary+DataFormat.h"
#import "NSMutableDictionary+Format.h"
#import "GEPickerTextField.h"
#import "NSMutableArray+Format.h"
#import "GEWeekTimeFromatter.h"
#import "GEDateFromatter.h"

@interface MCSearchChargeInViewController ()

@property (nonatomic, strong) NSMutableDictionary *orderInfo;

@end

@implementation MCSearchChargeInViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [self setTitle: @"数据筛选"];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.tableHeaderView = self.tableHeaderView;
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SuperWidth, 10)];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UIAlertView

- (void)showAlertView:(NSString*)message
{
    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"温馨提示" message:message delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
    alert.alertViewStyle=UIAlertViewStyleDefault;
    [alert show];
}


#pragma mark
#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = [NSString stringWithFormat:@"CreateContractViewController%ld%ld",indexPath.section,indexPath.row];
    
    //按年份
    if(indexPath.section == 0){
        GEInputTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell != nil) return cell;
        
        if(indexPath.row == 0){
            cell = [[GEInputTableViewCell alloc] initWithSelectReuseIdentifier:CellIdentifier request:nil];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.titleName = @"起始年份";
            cell.placeholder = @"请选择开始年份";
            cell.isMark = YES;
            [cell setEnablePrevious:NO next:NO];
            [cell setDatePickerMode:UIDatePickerModeDate];
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            
            NSString *dd = [GEDateFromatter formmatTimeDate:[NSDate date] format:@"yyyy"];
            cell.inputFiled = dd;
            
            NSMutableArray *array = [NSMutableArray array];
            for(int i= 1980; i< 2050; i++){
                NSString *showValue = [NSString stringWithFormat:@"%d",i];
                NSNumber *showNumber = [NSNumber numberWithInt:i];
                [array addObject:@{@"Definition":showValue,@"value":showNumber}];
            }
            NSDictionary *year = [NSDictionary dictionaryWithObject:@"%@" forKey:@"Definition"];
            [cell setPickerTextFieldComponents:array keyValue:@[year]];
            
        } else if(indexPath.row == 1){
            cell = [[GEInputTableViewCell alloc] initWithSelectReuseIdentifier:CellIdentifier request:nil];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.titleName = @"截止年份";
            cell.placeholder = @"请选择截止年份";
            cell.isMark = YES;
            [cell setEnablePrevious:NO next:NO];
            [cell setDatePickerMode:UIDatePickerModeDate];
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            
            NSString *dd = [GEDateFromatter formmatTimeDate:[NSDate date] format:@"yyyy"];
            cell.inputFiled = dd;
            
            NSMutableArray *array = [NSMutableArray array];
            for(int i= 1980; i< 2050; i++){
                NSString *showValue = [NSString stringWithFormat:@"%d",i];
                NSNumber *showNumber = [NSNumber numberWithInt:i];
                [array addObject:@{@"Definition":showValue,@"value":showNumber}];
            }
            NSDictionary *year = [NSDictionary dictionaryWithObject:@"%@" forKey:@"Definition"];
            [cell setPickerTextFieldComponents:array keyValue:@[year]];
        }
        return cell;
    }
    
    //按月份
    if(indexPath.section == 1){
        GEInputTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell != nil) return cell;
        
        if(indexPath.row == 0){
            cell = [[GEInputTableViewCell alloc] initWithSelectMultiReuseIdentifier:CellIdentifier];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.titleName = @"起始月份";
            cell.placeholder = @"请选择起始月份";
            cell.isMark = YES;
            cell.isEdit = NO;
            [cell setEnablePrevious:NO next:NO];
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            
            NSString *dd = [GEDateFromatter formmatTimeDate:[NSDate date] format:@"yyyy-MM"];
            cell.inputFiled = dd;
            
            NSMutableArray *TLDValue01 = [NSMutableArray array];
            for(int i= 1980; i< 2050; i++){
                NSString *showNumber = [NSString stringWithFormat:@"%d",i];
                [TLDValue01 addObject:showNumber];
            }
            
            NSMutableArray *TLDValue03 = [NSMutableArray array];
            for(int i= 1; i< 12; i++){
                NSString *showNumber = nil;
                if (i < 10)showNumber = [NSString stringWithFormat:@"0%d",i];
                else showNumber = [NSString stringWithFormat:@"%d",i];
                [TLDValue03 addObject:showNumber];
            }
            [cell setMultiComponents:@[TLDValue01,@[@"-"],TLDValue03]];
            
        } else if(indexPath.row == 1){
            cell = [[GEInputTableViewCell alloc] initWithSelectMultiReuseIdentifier:CellIdentifier];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.titleName = @"截止月份";
            cell.placeholder = @"请选择截止月份";
            cell.isMark = YES;
            cell.isEdit = NO;
            [cell setEnablePrevious:NO next:NO];
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            
            NSString *dd = [GEDateFromatter formmatTimeDate:[NSDate date] format:@"yyyy-MM"];
            cell.inputFiled = dd;
            
            NSMutableArray *TLDValue01 = [NSMutableArray array];
            for(int i= 1980; i< 2050; i++){
                NSString *showNumber = [NSString stringWithFormat:@"%d",i];
                [TLDValue01 addObject:showNumber];
            }
            
            NSMutableArray *TLDValue03 = [NSMutableArray array];
            for(int i= 1; i<= 12; i++){
                NSString *showNumber = nil;
                if (i < 10)showNumber = [NSString stringWithFormat:@"0%d",i];
                else showNumber = [NSString stringWithFormat:@"%d",i];
                [TLDValue03 addObject:showNumber];
            }
            [cell setMultiComponents:@[TLDValue01,@[@"-"],TLDValue03]];
        }
        return cell;
    }
    
    //按天数
    if(indexPath.section == 2){
        GEInputTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell != nil) return cell;
        
        if(indexPath.row == 0){
            cell = [[GEInputTableViewCell alloc] initWithDateReuseIdentifier:CellIdentifier];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.titleName = @"开始日期";
            cell.placeholder = @"请选择开始日期";
            cell.isMark = YES;
            [cell setEnablePrevious:NO next:NO];
            [cell setDatePickerMode:UIDatePickerModeDate];
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            
        } else if(indexPath.row == 1){
            cell = [[GEInputTableViewCell alloc] initWithDateReuseIdentifier:CellIdentifier];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.titleName = @"截止日期";
            cell.placeholder = @"请选择截止日期";
            cell.isMark = YES;
            [cell setEnablePrevious:NO next:NO];
            [cell setDatePickerMode:UIDatePickerModeDate];
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
        }
        return cell;
    }
    
    return nil;
}

#pragma mark
#pragma mark - UITableViewDelegate

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [tableView setLayoutMargins:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    //键盘事件处理
    UITextField *textField = [self.tableView getCellSaveClass:[UITextField class] pathForRow:indexPath.row inSection:indexPath.section];
    if (textField) [textField becomeFirstResponder];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 15;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(10, 0, SuperWidth - 20, 30)];
    view.backgroundColor = [UIColor clearColor];
    
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, SuperWidth - 20, 29.5)];
    lable.textColor = ThemeColorBlue;
    lable.backgroundColor = [UIColor whiteColor];
    lable.font = [UIFont boldSystemFontOfSize:15];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(SuperWidth - 65, 0, 60, 29.5);
    [button setTitle:@"确定" forState:UIControlStateNormal];
    [button setTitleColor:ThemeColorBlue forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:15];
    button.titleLabel.textAlignment = NSTextAlignmentRight;
    
    if (section == 0) {
        lable.text = @"  按年份搜索";
        [button addTarget:self action:@selector(yearSelector:) forControlEvents:UIControlEventTouchUpInside];
    } else if(section == 1){
        lable.text = @"  按月份搜索";
        [button addTarget:self action:@selector(monthSelector:) forControlEvents:UIControlEventTouchUpInside];
    } else if(section == 2){
        lable.text = @"  按天数搜索";
        [button addTarget:self action:@selector(daySelector:) forControlEvents:UIControlEventTouchUpInside];
    }
    [view addSubview:lable];
    [view addSubview:button];
    
    return view;
}

#pragma mark
#pragma mark - Button Event

//近一周
- (IBAction)recentlyOneWeekSelector:(id)selector
{
    NSString *today = [GEDateFromatter formmatTimeDate:[NSDate date] format:@"yyyy-MM-dd"];
    NSString *seventDay = [GEWeekTimeFromatter getDayTimeString:-6];
    if (self.backResultDelagate && [self.backResultDelagate respondsToSelector:@selector(backResultDataKey:dataValue:)]) {
        NSArray *array = @[seventDay, today];
        [self.backResultDelagate backResultDataKey:self.key dataValue:array];
    }
}

//近三月
- (IBAction)recentlyThereMonthSelector:(id)selector
{
    NSString *month = [GEDateFromatter formmatTimeDate:[NSDate date] format:@"yyyy-MM"];
    NSString *firstMonth = [GEWeekTimeFromatter getMonthTimeString:-2];
    if (self.backResultDelagate && [self.backResultDelagate respondsToSelector:@selector(backResultDataKey:dataValue:)]) {
        NSArray *array = @[firstMonth, month];
        [self.backResultDelagate backResultDataKey:self.key dataValue:array];
    }
}

//近半年
- (IBAction)recentlySixMonthSelector:(id)selector
{
    NSString *month = [GEDateFromatter formmatTimeDate:[NSDate date] format:@"yyyy-MM"];
    NSString *firstMonth = [GEWeekTimeFromatter getMonthTimeString:-5];
    if (self.backResultDelagate && [self.backResultDelagate respondsToSelector:@selector(backResultDataKey:dataValue:)]) {
        NSArray *array = @[firstMonth, month];
        [self.backResultDelagate backResultDataKey:self.key dataValue:array];
    }
}

//近一年
- (IBAction)recentlyOneYearSelector:(id)selector
{
    NSString *year = [GEDateFromatter formmatTimeDate:[NSDate date] format:@"yyyy"];
    NSString *firstYear = [GEWeekTimeFromatter getYearTimeString:-1];
    if (self.backResultDelagate && [self.backResultDelagate respondsToSelector:@selector(backResultDataKey:dataValue:)]) {
        NSArray *array = @[firstYear, year];
        [self.backResultDelagate backResultDataKey:self.key dataValue:array];
    }
}

#pragma mark
#pragma mark - Button Event

- (void)yearSelector:(id)selector
{
    UITextField *textField01 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:0 inSection:0];
    UITextField *textField02 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:1 inSection:0];
    
    //关闭页面，返回数据
    if (self.backResultDelagate && [self.backResultDelagate respondsToSelector:@selector(backResultDataKey:dataValue:)]) {
        NSArray *array = @[textField01.text, textField02.text];
        [self.backResultDelagate backResultDataKey:self.key dataValue:array];
    }
}

- (void)monthSelector:(id)selector
{
    UITextField *textField01 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:0 inSection:1];
    UITextField *textField02 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:1 inSection:1];
    
    //关闭页面，返回数据
    if (self.backResultDelagate && [self.backResultDelagate respondsToSelector:@selector(backResultDataKey:dataValue:)]) {
        NSArray *array = @[textField01.text, textField02.text];
        [self.backResultDelagate backResultDataKey:self.key dataValue:array];
    }
}

- (void)daySelector:(id)selector
{
    UITextField *textField01 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:0 inSection:2];
    UITextField *textField02 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:1 inSection:2];
    
    //关闭页面，返回数据
    if (self.backResultDelagate && [self.backResultDelagate respondsToSelector:@selector(backResultDataKey:dataValue:)]) {
        NSArray *array = @[textField01.text, textField02.text];
        [self.backResultDelagate backResultDataKey:self.key dataValue:array];
    }
}

- (void)backButtonEvent:(id)sender
{
    //关闭页面，返回数据
    if (self.backResultDelagate && [self.backResultDelagate respondsToSelector:@selector(backResultDataKey:dataValue:)]) {
        [self.backResultDelagate backResultDataKey:self.key dataValue:nil];
    }
}

@end
